package com.jack.bookshop.servlet;

import com.jack.bookshop.dao.UserDao;
import com.jack.bookshop.vo.ResultVo;
import com.jack.bookshop.vo.UserVo;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/user-signin.do")
public class UserSigninServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 设置编码格式，避免post提交表单的中文乱码问题
        request.setCharacterEncoding("utf-8");
        // 清空session中的userVo属性
        request.getSession().removeAttribute("userVo");
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String captcha = request.getParameter("captcha");
        // 从session中读取图形验证码字符串
        String code = request.getSession().getAttribute("validate").toString();
        // 验证用户输入
        ResultVo validateResultVo = this.validate(username, password, captcha, code);
        if(validateResultVo.getCode()==1) {
            // 调用dao验证用户名和密码，String.trim() 去除字符串前后的空格
            UserVo userVo = new UserDao().signin(username.trim(), password.trim());
            if(userVo!=null&&userVo.getId()>0) {
                // 用户名密码正确，设置session中的userVo属性
                request.getSession().setAttribute("userVo", userVo);
                // System.out.println(System.currentTimeMillis() + "\t" + username + "\t Sign In Success.");
            }else {
                //用户名密码错误，设置request中的resultVo属性
                ResultVo resultVo = new ResultVo();
                resultVo.fail();
                List<String> list = new ArrayList<String>();
                list.add("The username or password may be wrong, Please try again!");
                resultVo.setList(list);
                request.setAttribute("resultVo", resultVo);
                System.out.println(System.currentTimeMillis() + "\t" + username + "\t Sign In Fail.");
            }
        }else {
            request.setAttribute("resultVo", validateResultVo);
        }
        // 转发到结果页
        request.getRequestDispatcher("signin-result.jsp").forward(request, response);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }

    // 验证用户输入
    protected ResultVo validate(String username, String password, String captcha, String code) {
        ResultVo resultVo = new ResultVo();
        List<String> list = new ArrayList<String>();
        if(captcha!=null && code!=null && captcha.equalsIgnoreCase(code)) {
            if (username != null && username.length() <= 20 && username.length() >= 4) {
                // 通过
            }else {
                String s = "Username is too short or too long, it must be 4-20 characters long!";
                list.add(s);
            }
            if (password != null && password.length() <= 20 && password.length() >= 4) {
                // 通过
            }else {
                String s = "password is too short or too long, it must be 4-20 characters long!";
                list.add(s);
            }
        }else {
            String s = "Captcha is not correct, Please try again!";
            list.add(s);
        }
        if(list.size()>0) {
            resultVo.fail();
        }else {
            resultVo.success();
        }
        resultVo.setList(list);
        //System.out.println(resultVo);
        return resultVo;
    }

}
